﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.Resources;
using System.Reflection;
using DevExpress.XtraTab;
namespace QLTV
{
    public class BaseForm : DevExpress.XtraEditors.XtraForm
    {
        public DataSet Datasource { get; set; }
        public string UserName { get; set; }

        public string cItemKey { get; set; }
        public Permissions MyPermission { get; set; }
        public ResourceManager rm;
        public System.Globalization.CultureInfo myCultureInfo;

        public delegate void RefreshData();
        public RefreshData _refreshData;
        public delegate void loadActivedEvent();
        public event loadActivedEvent _activedEvent;

        #region show waiting message
        protected Forms.frmPleaseWait _wait;
        protected void RunLongThread(ThreadStart loaddatta)
        {
            lock (this)
            {
                Thread lengthyProcessThread = new Thread(loaddatta) { IsBackground = true };
                lengthyProcessThread.Start();
                _wait = new Forms.frmPleaseWait();
                _wait.ShowDialog();
            }
        }
        #endregion

        public BaseForm()
        {
            if (!String.IsNullOrEmpty(Program.language))
            {
                myCultureInfo = new CultureInfo(Program.language);
                Thread.CurrentThread.CurrentUICulture = myCultureInfo;
            }
            rm = new ResourceManager("QLTV.Properties.Resources", Assembly.GetExecutingAssembly());
        }
        protected override void OnLoad(EventArgs e)
        {
            if (Datasource != null)
            {
                using (QLTV.Database.dsPermissionTableAdapters.tblQuyenTableAdapter tadQuyen = new Database.dsPermissionTableAdapters.tblQuyenTableAdapter())
                {
                    cItemKey = this.Name;
                    UserName = Program.username;
                    #region "kiem tra neu la form frmCrewList"
                    if (this.Name == "frmCrewList")
                    {
                        using (Database.dsPermission.tblQuyenDataTable dt = new Database.dsPermission.tblQuyenDataTable())
                        {
                            tadQuyen.LayQuyen((QLTV.Database.dsPermission.tblQuyenDataTable)(dt), UserName, "frmCrew");
                            List<Permissions> listControl = (from per in dt.AsEnumerable()
                                                             select new Permissions
                                                             {
                                                                 Sua = per.Field<Boolean>("bSua"),
                                                                 TruyCap = per.Field<Boolean>("bTruyCap"),
                                                                 ControlInMain = per.Field<string>("ControlInMain")
                                                             }).ToList<Permissions>();
                            foreach (Permissions item in listControl)
                            {
                                object tabPage = this.GetType().GetField(item.ControlInMain).GetValue(this);
                                tabPage.GetType().GetProperty("PageEnabled").SetValue(tabPage, item.TruyCap, null);
                            }
                        }

                    }

                    #endregion
                    //use databinding to set permission
                    tadQuyen.LayQuyen((QLTV.Database.dsPermission.tblQuyenDataTable)(Datasource.Tables[0]), UserName, cItemKey);
                    if (Datasource.Tables[0].Rows.Count > 1)
                    {
                        //truong hop phan quyen tren tung control cua cItemKey
                        List<Permissions> listControl = (from per in Datasource.Tables[0].AsEnumerable()
                                                         select new Permissions
                                                         {
                                                             Sua = per.Field<Boolean>("bSua"),
                                                             TruyCap = per.Field<Boolean>("bTruyCap"),
                                                             ControlInMain = per.Field<string>("ControlInMain")
                                                         }).ToList<Permissions>();
                        foreach (Permissions item in listControl)
                        {
                            object tabPage = this.GetType().GetField(item.ControlInMain).GetValue(this);
                            //PHan quyen cho tab

                            tabPage.GetType().GetProperty("PageEnabled").SetValue(tabPage, item.TruyCap, null);
                            foreach (Control _control in ((XtraTabPage)tabPage).Controls)
                            {

                                _control.GetType().GetProperty("Enabled").SetValue(_control, item.Sua, null);

                            }

                        }
                    }
                    else if (Datasource.Tables[0].Rows.Count == 1)
                    {
                        //or use by code
                        Permissions query = (from per in Datasource.Tables[0].AsEnumerable()
                                             select new Permissions
                                             {
                                                 isAdmin = per.Field<Boolean>("isAdmin"),
                                                 username = per.Field<string>("cUserName"),
                                                 formKey = per.Field<string>("cItemKey"),
                                                 Full = per.Field<Boolean>("bFull"),
                                                 TruyCap = per.Field<Boolean>("bTruyCap"),
                                                 Them = per.Field<Boolean>("bThem"),
                                                 Sua = per.Field<Boolean>("bSua"),
                                                 Xoa = per.Field<Boolean>("bXoa"),
                                                 In = per.Field<Boolean>("bIn"),
                                                 Nhap = per.Field<Boolean>("bNhap"),
                                                 Xuat = per.Field<Boolean>("bXuat")

                                             }).SingleOrDefault<Permissions>();
                        this.MyPermission = query;
                    }
                }
            }
            base.OnLoad(e);
        }
        public void raiseActived()
        {
            if (_activedEvent != null)
                _activedEvent();
        }
        protected override void OnActivated(EventArgs e)
        {
            raiseActived();
        }
    }
    public class MainBase : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public DataSet Datasource { get; set; }
        public string UserName { get; set; }
        public string cItemKey { get; set; }
        public Permissions MyPermissionPerForm { get; set; }
        public List<Permissions> MyFullPermission { get; set; }
        public ResourceManager rm;
        public System.Globalization.CultureInfo myCultureInfo;
        public MainBase()
        {
            if (!String.IsNullOrEmpty(Program.language))
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(Program.language);
            rm = new ResourceManager("QLTV.Properties.Resources", Assembly.GetExecutingAssembly());
        }


        protected override void OnLoad(EventArgs e)
        {
            if (Datasource != null)
            {
                using (QLTV.Database.dsPermissionTableAdapters.tblQuyenTableAdapter tadQuyen = new Database.dsPermissionTableAdapters.tblQuyenTableAdapter())
                {

                    UserName = Program.username;
                    //use databinding to set permission
                    tadQuyen.Fill((QLTV.Database.dsPermission.tblQuyenDataTable)(Datasource.Tables[0]), UserName);
                    //or use by code
                    List<Permissions> listControl = (from per in Datasource.Tables[0].AsEnumerable()
                                                     select new Permissions
                                                     {
                                                         TruyCap = per.Field<Boolean>("bTruyCap"),
                                                         ControlInMain = per.Field<string>("ControlInMain")

                                                     }).ToList<Permissions>();
                    foreach (Permissions item in listControl)
                    {
                        if (this.GetType().GetField(item.ControlInMain) != null)
                        {
                            object button = this.GetType().GetField(item.ControlInMain).GetValue(this);
                            button.GetType().GetProperty("Enabled").SetValue(button, item.TruyCap, null);
                        }

                    }
                }
            }
            base.OnLoad(e);
        }


    }
    public class UserControlBase : DevExpress.XtraEditors.XtraUserControl
    {
        public DataSet Datasource { get; set; }
        public string UserName { get; set; }

        public string cItemKey { get; set; }
        public Permissions MyPermission { get; set; }
        protected override void OnLoad(EventArgs e)
        {
            if (Datasource != null && Program.username != "")
            {
                using (QLTV.Database.dsPermissionTableAdapters.tblQuyenTableAdapter tadQuyen = new Database.dsPermissionTableAdapters.tblQuyenTableAdapter())
                {
                    cItemKey = this.ParentForm.Name;
                    UserName = Program.username;
                    //use databinding to set permission
                    tadQuyen.LayQuyen((QLTV.Database.dsPermission.tblQuyenDataTable)(Datasource.Tables[0]), UserName, cItemKey);
                    //or use by code
                    Permissions query = (from per in Datasource.Tables[0].AsEnumerable()
                                         select new Permissions
                                         {
                                             isAdmin = per.Field<Boolean>("isAdmin"),
                                             username = per.Field<string>("cUserName"),
                                             formKey = per.Field<string>("cItemKey"),
                                             Full = per.Field<Boolean>("bFull"),
                                             TruyCap = per.Field<Boolean>("bTruyCap"),
                                             Them = per.Field<Boolean>("bThem"),
                                             Sua = per.Field<Boolean>("bSua"),
                                             Xoa = per.Field<Boolean>("bXoa"),
                                             In = per.Field<Boolean>("bIn"),
                                             Nhap = per.Field<Boolean>("bNhap"),
                                             Xuat = per.Field<Boolean>("bXuat")

                                         }).SingleOrDefault<Permissions>();
                    this.MyPermission = query;
                }
            }
            base.OnLoad(e);
        }


    }
    public class Permissions
    {
        public string username { get; set; }
        public Boolean isAdmin { get; set; }
        public string formKey { get; set; }
        public Boolean Full { get; set; }
        public Boolean TruyCap { get; set; }
        public Boolean Them { get; set; }
        public Boolean Sua { get; set; }
        public Boolean Xoa { get; set; }
        public Boolean In { get; set; }
        public Boolean Nhap { get; set; }
        public Boolean Xuat { get; set; }
        public string ControlInMain { get; set; }
    }
}
